#include "Item.h"

// Create & Destroy Function //

Item::Item():
	title(), description(), link(), publishDate(), author(), tags(20)
{
	this->channel = NULL;
	this->countOftags = 0;
}

Item::Item( string title, string description, string link, Date publishDate, string author, Channel* channel ) :
	title( title ), description ( description ), link ( link ), publishDate( publishDate ), author ( author ), tags(20)
{
	this->channel = channel;
	this->countOftags = 0;
}

Item::Item( const Item& source ) :
	title ( source.title), description ( source.description ), link ( source.link ),
	publishDate( source.publishDate ), author ( source.author ), tags(source.tags)
{
	this->channel = source.channel;
	this->countOftags = source.countOftags;
}

Item::~Item()
{
}


// Operator Function //

Item& Item::operator = ( const Item& source )
{
	this->title = source.title;
	this->description = source.description;
	this->link = source.link;
	this->publishDate = const_cast<Date&>(source.publishDate);
	this->author = source.author;
	this->channel = source.channel;
	this->countOftags = source.countOftags;
	this->tags = source.tags;

	return (*this);
}


bool Item::operator == ( const Item& other )
{
	bool ret = false;

	if ( this->title == other.title &&
		 this->description == other.description &&
		 this->link == other.link &&
		 this->publishDate == other.publishDate &&
		 this->author == other.author &&
		 this->channel == other.channel &&
		 this->countOftags == other.countOftags &&
		 this->tags == other.tags
	   )
	{
		ret = true;
	}

	return ret;
}


bool Item::operator != ( const Item& other )
{
	bool ret = true;

	if ( this->title == other.title &&
		 this->description == other.description &&
		 this->link == other.link &&
		 this->publishDate == other.publishDate &&
		 this->author == other.author &&
		 this->channel == other.channel &&
		 this->countOftags == other.countOftags &&
		 this->tags == other.tags
	   )
	{
		ret = false;
	}

	return ret;
}

Tag* Item::operator [] ( int index )
{
	return const_cast<Tag*>(this->tags[index]);
}

int Item::Record(Tag* tag)
{
	int ret = -1;
	int index = this->tags.Store(this->countOftags, tag);
	if( index > -1)
	{
		ret = index;
		(this->countOftags)++;
	}

	return ret;
}

int CompareByTagTitle(void* one, void* other)
{
	int ret = -1;
	if( ((Tag*)one)->GetTitle() == *((string*)other) )
	{
		ret = 0;
	}
	return ret;
}

int Item::Find( string title )
{
	int ret = -1;

	int index = this->tags.LinearSearchUnique( &title, CompareByTagTitle );
	if(index > -1)
	{
		ret = index;
	}

	return ret;
}
